﻿using JHECIS.DataModel;
using JHECIS.DataModel.Interface;
using JhEngine.Helpers;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;

namespace JHECIS.InterfaceToolDataService
{
    public class CommonResult
    {
        public bool Result { get; set; } = true;

        public string ErrMsg { get; set; }

        public object ReturnData { get; set; }
    }
    public class InterfaceToolService
    {
        #region 单例

        ///// <summary>
        /////     延迟加载对象
        ///// </summary>
        //private static readonly Lazy<InterfaceToolService> _lazy =
        //    new Lazy<InterfaceToolService>(() => new InterfaceToolService(), LazyThreadSafetyMode.PublicationOnly);

        private readonly IRestClient client;

        public InterfaceToolService(IRestClient client)
        {
            this.client = client;
        }

        ///// <summary>
        /////     单例对象
        ///// </summary>
        //public static InterfaceToolService Instance => _lazy.Value;

        #endregion

        public string Convert(ScriptContent script)
        {
            var result = string.Empty;
            try
            {

                var resource = "api/JavaScript/Convert";
                if (script.ScriptType == 1)
                {
                    resource = "api/PythonScript/Convert";
                }
                var request = new RestRequest(resource, Method.POST);
                var json = JsonConvert.SerializeObject(script);
                LogHelper.GetLogHelper().Trace("接口参数：" + script.Data);
                request.AddJsonBody(json); // replaces matching token in request.Resource

                var response = client.Execute(request);
                var responseApi = JsonConvert.DeserializeObject<ResponseApi<string>>(response.Content);
                result = responseApi.data;

            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
            }

            return result;
        }

        public InterfaceToolEntity GetInterfaceToolByCode(string interfaceCode)
        {
            var result = new InterfaceToolEntity();
            try
            {
                var request = new RestRequest("api/InterfaceTool/GetInterfaceToolByCode", Method.GET);
                request.AddParameter("interfaceCode", interfaceCode);

                var response = client.Execute(request);
                var responseApi = JsonConvert.DeserializeObject<ResponseApi<InterfaceToolEntity>>(response.Content);
                result = responseApi.data;
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
            }

            return result;
        }

        public List<InterfaceToolEntity> GetInterfaceTool()
        {
            var result = new List<InterfaceToolEntity>();
            try
            {
                var request = new RestRequest("api/InterfaceTool/GetInterfaceTool", Method.GET);

                var response = client.Execute(request);
                var responseApi = JsonConvert.DeserializeObject<ResponseApi<List<InterfaceToolEntity>>>(response.Content);
                result = responseApi.data;
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
            }

            return result;
        }

        public ResponseApi<CommonResult> SaveInTerfaceTool(List<InterfaceToolEntity> interfaceToolList)
        {
            var result = new ResponseApi<CommonResult>();
            try
            {
                if (interfaceToolList.Any())
                {
                    var request = new RestRequest("api/InterfaceTool/SaveInterfaceTool", Method.POST);

                    var json = JsonConvert.SerializeObject(interfaceToolList);
                    request.AddJsonBody(json);

                    var response = client.Execute(request);
                    result = JsonConvert.DeserializeObject<ResponseApi<CommonResult>>(response.Content);
                }

            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
                LogHelper.GetLogHelper().Error(ex);
            }

            return result;
        }
    }
}
